/**
 * 
 */
package com.dp.util;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import com.dp.dao.TerminalDao;
import com.dp.pojo.MobileCompany;
import com.dp.pojo.TerminalPO;

/**
 * 品牌缓存
 * @author dingdaiyang
 * 
 */
public class ComCache {
	
	private static final Logger log = Logger.getLogger(ComCache.class);
	// 品牌的区域缓存  区域ID：品牌列表
	private Map<Integer,List<MobileCompany>> area_list = new TreeMap<Integer,List<MobileCompany>>();
	// 品牌的ID名称缓存  品牌ID：品牌名字
	private Map<Long,String> id_name = new TreeMap<Long,String>();
	// 品牌的ID名称缓存  品牌ID：品牌
	private Map<Long,MobileCompany> id_com = new TreeMap<Long,MobileCompany>();
	// 品牌列表缓存  
	private List<MobileCompany> list = new LinkedList<MobileCompany>();
	// 品牌的排名缓存   品牌排名：品牌
	private Map<Integer,MobileCompany> rank_map = new TreeMap<Integer,MobileCompany>(new Comparator<Integer>(){
		public int compare(Integer o1, Integer o2) {
			if(o1==null){
				if(o2==null){
					return  0;
				}else{
					return 1;
				}
			}else{
				if(o2==null){
					return  -1;
				}else{
					return o1 - o2;
				}
			}
		}
	});
	// 品牌的开头字符相应机型的缓存  品牌ID：（开头字符：机型列表）
	private Map<Long, Map<String,List<TerminalPO>>> com_start_map = new TreeMap<Long, Map<String,List<TerminalPO>>>();

	private TerminalDao terminalDao;
	
	public ComCache(TerminalDao terminalDao) {
		this.terminalDao = terminalDao;
		load();
	}
	
	
	public void reload() {
		
		load();
	}
	
	
	/**
	 * 加载缓存
	 */
	private synchronized void load() {
		
		List<MobileCompany> com_list = null;
		MobileCompany com = new MobileCompany();
		ComparatorModel comparator = new ComparatorModel();
		
		try {
			com_list = terminalDao.getCompanyList();
			
			if(com_list != null && com_list.size() > 0){
				Map<Integer,MobileCompany> rank_tmp = new TreeMap<Integer,MobileCompany>(new Comparator<Integer>(){
					public int compare(Integer o1, Integer o2) {
						if(o1==null){
							if(o2==null){
								return  0;
							}else{
								return 1;
							}
						}else{
							if(o2==null){
								return  -1;
							}else{
								return o1 - o2;
							}
						}
					}
				});
				String[] start, sub_start = null; // 存品牌开头字符
				int com_num, start_num = 0;
				list = com_list; // 缓存了所有品牌的列表
				com_num = com_list.size();
				
				// 缓存了 地区：品牌列表
				Map<Integer,List<MobileCompany>> area_tmp = new TreeMap<Integer,List<MobileCompany>>();
				for (int j = 1; j < 4; j++){
					List<MobileCompany> area_list_tmp = terminalDao.getMCompanysByArea(j);
					area_tmp.put(j, area_list_tmp);
				}
				
				Map<Long,String> id_tmp = new TreeMap<Long,String>();
				Map<Long,MobileCompany> id_com_tmp = new TreeMap<Long,MobileCompany>();
				Map<Long, Map<String,List<TerminalPO>>> com_map_tmp = new TreeMap<Long, Map<String,List<TerminalPO>>>();// 品牌ID：（开头字符：机型列表）
				List<TerminalPO> start_list_tmp = null;
				
				for (int i = 0; i < com_num; i++) { // 开始循环厂商
					Map<String,List<TerminalPO>> start_map_tmp = new TreeMap<String,List<TerminalPO>>(); // 开头字符：机型列表
					// 缓存了 品牌ID：品牌名称
					com = (MobileCompany)getList(com_list, i, com);
					id_tmp.put(com.getCom_id(), com.getCom_name()); // 品牌ID名称缓存
					// 缓存了 品牌ID：品牌
					id_com_tmp.put(com.getCom_id(), com); 
					// 缓存了 品牌排行：品牌
					rank_tmp.put(com.getRanking(), com);
					
					// 缓存了 开头字符：机型列表
					if(com.getUa_first_character() != null){
						start = com.getUa_first_character().split(",");
						start_num = start.length;
						for (int j = 0; j < start_num; j++) {
							List<TerminalPO> start_list = new LinkedList<TerminalPO>();// 存品牌开头字符相应列表
							sub_start  = start[j].split("/");
							for (int k = 0; k < sub_start.length; k++) {
								start_list_tmp = terminalDao.getTerminalListByShort(com.getCom_id(), sub_start[k]);
								start_list.addAll(start_list_tmp);
							}
							start_map_tmp.put(start[j], start_list); // 缓存了开头字符：机型列表
						}
					}
					start_list_tmp = terminalDao.getTerminalListByShort(com.getCom_id(), "");
					Collections.sort(start_list_tmp, comparator);
					start_map_tmp.put("all", start_list_tmp);
					
					com_map_tmp.put(com.getCom_id(), start_map_tmp); // 缓存了品牌ID：（开头字符：机型列表）
				}
				
				area_list = area_tmp; // 按区域编号缓存了相应品牌列表
				id_name   = id_tmp;   // 按品牌ID缓存了品牌名称
				id_com    = id_com_tmp; // 按品牌ID缓存了品牌
				rank_map = rank_tmp; // 按品牌的排名缓存了品牌列表
				com_start_map = com_map_tmp; // 按品牌ID缓存了开头字符对应的机型列表
			}
		} catch (SQLException e) {
			log.error("\t getCompanyList " + e);
		}
	}
	
	
	/**
	 * 优化
	 * @param list
	 * @param i
	 * @param sd
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private Object getList(List list, int i, Object sd){
		sd = list.get(i);
		return sd;
	}


	public List<MobileCompany> getList() {
		return list;
	}

	public Map<Integer, List<MobileCompany>> getArea_list() {
		return area_list;
	}
	
	public Map<Integer, MobileCompany> getRank_map() {
		return rank_map;
	}

	public Map<Long, String> getId_name() {
		return id_name;
	}
	
	public Map<Long, MobileCompany> getId_com() {
		return id_com;
	}
	
	public Map<Long, Map<String, List<TerminalPO>>> getCom_start_map() {
		return com_start_map;
	}
	
	class ComparatorModel implements Comparator{
		public int compare(Object arg0, Object arg1) {
			TerminalPO model0=(TerminalPO)arg0;
			TerminalPO model1=(TerminalPO)arg1;
			//比较文件大小
			int flag = model0.getShort_model().compareTo(model1.getShort_model());
			if( flag == 0 ){
				return model0.getShort_model().compareTo(model1.getShort_model());
			}else{
				return flag;
			}  
		}	 
	}

}